package yxy.game.pm2.command.passbarrier;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.passbariercost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.Date;


/****************************************
 * @name CMD_PassBarrierChallengeRefresh
 * @description 斩关夺隘主刷新
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierChallengeRefresh, classtype = PassBarrierMessage.PassBarrierChallengeRefreshRequest.class)
public class CMD_PassBarrierChallengeRefresh extends Command<PassBarrierMessage.PassBarrierChallengeRefreshRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengeRefreshRequest request) throws Exception {
        Date date = new Date();


        String playerCode = messageSource.getTag();
        boolean useSkip = request.getUseSkip();
        PassBarrierMessage.PassBarrierChallengeRefreshResponse.Builder response = PassBarrierMessage.PassBarrierChallengeRefreshResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            passbariercost data = DataCenter.getData(1, passbariercost.class);
            int refreshTimes = module.getRefreshTimes();
            if (refreshTimes > data.getLimit()) {
                send(messageSource, response, Result.PassBarrierChallengeRefreshReachLimit);
                return;
            }

            //todo 前三次免费，后续如果cd过了就免费，否则要花钱刷新
            Boolean isNeedCost = true;
            int lessFreerefreshTimes = module.getLessFreerefreshTimes();
            if (lessFreerefreshTimes > 0 || module.getRefreshTime() == null) {
                isNeedCost = false;
            }

            if (module.getRefreshTime() != null && module.getRefreshTime().isBefore(DateTime.now())) {
                isNeedCost = false;
            }

            if (!useSkip && isNeedCost) {
                send(messageSource, response, Result.PassBarrierCantRefresh);
                return;
            }

            //todo 消耗
            if (isNeedCost && useSkip) {
                Currency currency = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() + refreshTimes * data.getExtra());
                try {
                    new CurrencyHandle().cost(playerCode, CCReason.斩关夺隘, currency);
                } catch (CurrencyNotEnoughException e) {
                    Result result = new Result(Result.CostFail, e.getType().getNumber(), e.getDataId());// 消费不足
                    send(messageSource, response, result);
                    return;
                }
            }

            //todo
            if (lessFreerefreshTimes > 0) {
                module.setLessFreerefreshTimes(lessFreerefreshTimes - 1);
            }

            //todo 第三次免费的同时就要更新刷新时间了，第四次就要等待cd或购买扣减cd
            if (lessFreerefreshTimes - 1 <= 0) {
                module.setRefreshTime(DateTime.now().plusMinutes(PassbarrierPlayerModule.refreshMinute));
                module.setRefreshTimes(refreshTimes + 1);
            }

            //todo 重置怪物
            module.getMonsterByDegree();

            module.set();

            PassbarrierHandle handle = new PassbarrierHandle();

            //返回主页面信息
            PassBarrierMessage.PassBarrierInfoMsg.Builder passBarrierInfo = handle.getPassBarrierInfo(module);

            response.setMsgData(passBarrierInfo);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }


        Date date1 = new Date();
        System.out.println("CMD_PassBarrierChallengeRefresh:" + (date1.getTime() - date.getTime()));
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengeRefreshResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
